package com.wjpeng.wms.web.controller;

import com.wjpeng.wms.domain.StockOutcomeBill;
import com.wjpeng.wms.query.StockOutcomeBillQueryObject;
import com.wjpeng.wms.service.IClientService;
import com.wjpeng.wms.service.IDepotService;
import com.wjpeng.wms.service.IStockOutcomeBillService;
import com.wjpeng.wms.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("stockOutcomeBill")
public class StockOutcomeBillController {
    @Autowired
    private IStockOutcomeBillService stockOutcomeBillService;

    @Autowired
    private IDepotService depotService;

    @Autowired
    private IClientService clientService;

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StockOutcomeBillQueryObject qo) {
        model.addAttribute("result",stockOutcomeBillService.result(qo));
        model.addAttribute("depots",depotService.list());
        model.addAttribute("clients",clientService.list());
        return "stockOutcomeBill/list";
    }

    @RequestMapping("input")
    public String input(Model model,Long id){
        if(id != null){
            model.addAttribute("entity",stockOutcomeBillService.get(id));

        }
        model.addAttribute("depots",depotService.list());
        model.addAttribute("clients",clientService.list());
        return "stockOutcomeBill/input";
    }

    @RequestMapping("view")
    public String view(Model model,Long id){
        if(id != null){
            model.addAttribute("entity",stockOutcomeBillService.get(id));

        }
        model.addAttribute("depots",depotService.list());
        model.addAttribute("clients",clientService.list());
        return "stockOutcomeBill/view";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        if(id != null){
            stockOutcomeBillService.delete(id);
        }
        return new JSONResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StockOutcomeBill stockOutcomeBill) throws Exception{
        stockOutcomeBillService.saveOrUpdate(stockOutcomeBill);
        return new JSONResult();
    }

    @RequestMapping("audit")
    @ResponseBody
    public Object audit(Long id) throws Exception{
        JSONResult result = new JSONResult();
        try {
            stockOutcomeBillService.audit(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


}
